package MWClient;

import java.util.HashMap;
import java.util.Map;

import org.json.simple.JSONValue;

/**
 * <pre>
 * @Package : MWClient
 * @File    : MWClient.java
 * <pre>
 *
 * @Author  : 차재우
 * @Date    : 2013. 7. 22.
 * @Version :
 */
public class MWClient {
	private ConfigManager configManager = null;
	private ClientManager clientManager = null;
	
	// 시리얼 통신 객체
	private SerialComm serialComm = null;
	
	// 시리얼 송신 대기 큐
	public static SerialQueue sendQueue = new SerialQueue();
	
	// 시리얼 수신 대기 큐
	public static SerialQueue recvQueue = new SerialQueue();

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		new MWClient();
	}
	
	/**
	 * <pre>
	 * @Comment : 생성자
	 * <pre>
	 *
	 */
	public MWClient() {
		configManager = new ConfigManager();
		System.out.println(configManager.toString());

		String serialPort = configManager.getSerialPort();
		String serverIp = configManager.getServerIp();
		int serverPort = configManager.getServerPort();
		
		InsertTestData();

		try {
			clientManager = new ClientManager(serverIp, serverPort);
			clientManager.StartClient();
			
			this.serialComm = new SerialComm(serialPort);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * <pre>
	 * @Comment : 테스트 함수 (큐에 임시 데이터 생성)
	 * <pre>
	 *
	 * @Method  : InsertTestData
	 */
	public void InsertTestData() {
		Map<String, String> obj = new HashMap<String, String>();

		obj.put("TRNO", "RM01");
		obj.put("ITYPE", "O");
		obj.put("CSERNO", "S12345");
		obj.put("BTHYMD", "19811220");
		obj.put("SEXCD", "M");
		obj.put("DEPTCD", "01");
		obj.put("ISUNO", "1234");
		obj.put("ISUDT", "20130701");
		obj.put("IOTYPE", "I");

		String jsonStr = JSONValue.toJSONString(obj);
		byte[] data = jsonStr.getBytes();

		recvQueue.Push(data);
		recvQueue.Push(data);

		System.out.println("RecvQueue size : " + recvQueue.GetQueueSize());
	}
}
